package com.woniuxy.amusementparkos.controller.lzw;


import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.lzw.ShiftExchangeInfo;
import com.woniuxy.amusementparkos.entity.lzw.StatusInfo;
import com.woniuxy.amusementparkos.entity.lzw.UserAdjustMoneyInfo;
import com.woniuxy.amusementparkos.entity.wyh.Customer;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Info;
import com.woniuxy.amusementparkos.exception.lzw.KnownException;
import com.woniuxy.amusementparkos.exception.lzw.unifyhanding.code.enumwarehouse.PublicEnumCode;
import com.woniuxy.amusementparkos.service.lzw.StatusInfoService;
import com.woniuxy.amusementparkos.service.wyh.CustomerService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_InfoService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;
import com.woniuxy.amusementparkos.service.lzw.UserAdjustMoneyInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * (UserAdjustMoneyInfo)表控制层
 *
 * @author lizhiwen
 * @since 2020-04-26 11:37:08
 */
@RestController
@RequestMapping("userAdjustMoneyInfo")
public class UserAdjustMoneyInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserAdjustMoneyInfoService userAdjustMoneyInfoService;

    @Resource
    WristStrap_InfoService wristStrap_infoService;

    /**
     * 顾客信息 服务
     */
    @Resource
    private CustomerService customerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UserAdjustMoneyInfo selectOne(Integer id) {
        return this.userAdjustMoneyInfoService.getById(id);
    }

    /**
     * 插入与用户关联的腕带费用调整信息
     *
     *
     * @param userAdjustMoneyInfo 用户腕带费用调整信息
     * @return null：代表无问题
     */
    @PostMapping("insert")
    public ResponseResult<UserAdjustMoneyInfo> insertUserAdjustMoneyInfo(UserAdjustMoneyInfo userAdjustMoneyInfo){
        if(userAdjustMoneyInfo == null){
            throw new KnownException(PublicEnumCode.NPE);
        }
        // 获取腕带编号
        String wdId = userAdjustMoneyInfo.getWdId();
        // 通过编号查询腕带信息
        WristStrap_Info wristStrap_info = this.wristStrap_infoService.queryByWristStrap_Id(wdId);
        // 执行插入，并表示扣钱
        ResponseResult<UserAdjustMoneyInfo> result = new ResponseResult<>(
                PublicEnumCode.SUCCESS,
                this.userAdjustMoneyInfoService.insert(userAdjustMoneyInfo));
        // 程序执行扣钱操作
        BigDecimal newbalance = wristStrap_info.getBalance().add(userAdjustMoneyInfo.getAdjustMoney());
        // 判空
        if(newbalance == null){
            throw new KnownException(PublicEnumCode.NPE);
        }
        // 更改金额
        this.wristStrap_infoService.changeBalacne(
                wristStrap_info.getInfo_id(), newbalance);
        return result;
    }

    /**
     * 通过 腕带编号 查询相关腕带费用调整信息
     *
     * @param wdId 腕带编号
     * @return 该腕带编号下的所有费用调整信息
     */
    @GetMapping("listBywdId")
    public ResponseResult<List<UserAdjustMoneyInfo>> listByWdId(String wdId){
        ResponseResult<List<UserAdjustMoneyInfo>> result = null;
        try {
            result = new ResponseResult<>(200,"成功",userAdjustMoneyInfoService.listByWdId(wdId));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

    /**
     * 通过 腕带编号+腕带使用用户 查询相关腕带费用调整信息
     *
     * @param wdId 腕带编号
     * @param wdUser 腕带使用用户
     * @return 该腕带编号+腕带使用用户下的所有费用调整信息
     */
    @PostMapping("listBywdIdAndwdUser")
    public ResponseResult<List<UserAdjustMoneyInfo>> listByWdIdAndWdUser(String wdId,Integer wdUser){
        ResponseResult<List<UserAdjustMoneyInfo>> result = null;
        try {
            result = new ResponseResult<>(200,"成功",userAdjustMoneyInfoService.listByWdIdAndWdUser(wdId,wdUser));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

    /**
     * 更改 腕带费用信息
     *    主要更改腕带费用信息是否处于【已执行】状态
     *
     * @param userAdjustMoneyInfo 用户调整腕带费用信息
     * @return 返回 已更改的UserAdjustMoneyInfo
     */
    @PostMapping("update")
    public ResponseResult<UserAdjustMoneyInfo> updateUserAdjustMoneyInfo(UserAdjustMoneyInfo userAdjustMoneyInfo){
        ResponseResult<UserAdjustMoneyInfo> result = null;
        try {
            result = new ResponseResult<>(200,"成功",userAdjustMoneyInfoService.update(userAdjustMoneyInfo));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

    /**
     * 删除 用户腕带费用配置插入的 信息
     *     真删除
     *
     * @param id
     * @return
     */
    @PostMapping("delete")
    public ResponseResult<Boolean> delete(Integer id){

        ResponseResult<Boolean> result = null;
        try {
            result = new ResponseResult<>(200,"成功",userAdjustMoneyInfoService.deleteById(id));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

    /**
     * 额外：需要通过id，获取顾客信息
     *
     * @param customer_id 顾客信息id
     * @return 顾客信息
     */
    @GetMapping("getByCustomerId")
    public ResponseResult<Customer> getByCustomerId(Integer customer_id){
        ResponseResult<Customer> result = null;
        try {
            result = new ResponseResult<>(200,"成功",customerService.getById(customer_id));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

    /**
     * 额外：需要通过 顾客信息中 的名字 获取相关数据
     *     虽返回为list，但对于前台操作，我们只取第一个list元素
     * @param customer
     * @return
     */
    @PostMapping("getByCustomer")
    public ResponseResult<List<Customer>> listByCustomerInfo(Customer customer){
        ResponseResult<List<Customer>> result = null;
        try {
            result = new ResponseResult<>(200,"成功",customerService.getAllBy(customer));
        }catch (Exception e){
            result = new ResponseResult<>(502,e.getMessage(),null);
        }
        return result;
    }

}